const { exec, execSync } = require('child_process')

const command = {
  async exec (command, stdoutfn, stderrfn, args = []) {
    let stdout = null, stderr = null
    return new Promise((resolve, reject) => {
      const child = exec(command, args)
      child.stdout.on('data', (buffer) => {
        stdout = buffer
      })
      child.stderr.on('data', (buffer) => {
        stderr = buffer
      })
      child.on('close', function (code) {
        if (code == 0) {
          resolve(stdout, stderr)
        } else {
          console.log(`command failed: ${command} ${args.join(' ')}`)
          reject(code)
        }
      })
      child.on('exit', (code) => {
        // console.log('退出: ' + code)
      })
    })
  },
  execSync
}

module.exports = command
